package demo1;

class Outerclass {//外部类
    public int data1 = 1;
    private int data2 = 2;//属于对象
    public static int data3 = 3;

    //静态内部类
    static class InnerClass {
        public int data4 = 4;
        private int data5 = 5;
        public static int data6 = 6;

        public void func() {
            System.out.println("static->InnerClass::func()");
            //System.out.println(data1);//所有静态的都不依赖于对象
            //静态内部类当中 不能直接访问外部类的非静态成员
            Outerclass outerclass = new Outerclass();
            System.out.println(outerclass.data1);
            System.out.println(outerclass.data2);
            System.out.println(data3);
            System.out.println(this.data4);
            System.out.println(data5);
            System.out.println(data6);
        }
    }
    public void test() {
        InnerClass innerClass = new InnerClass();
        System.out.println(data1);
        System.out.println(data2);
        System.out.println(data3);
        System.out.println(innerClass.data4);
        System.out.println(innerClass.data5);//外部类可以访问静态内部类当中的所有成员
        System.out.println(InnerClass.data6);
    }
}
class Outerclass2 {//外部类
    public int data1 = 1;
    private int data2 = 2;//属于对象
    public static int data3 = 3;

    //非静态内部类
    class InnerClass2 {
        public int data1 = 1111;
        public int data4 = 4;
        private int data5 = 5;
        //此时当外部类加载的时候 这个非静态内部类不会加载
        public static final int data6 = 6;//在实例内部类当中，不能定义静态的成员变量

        public void func() {
            System.out.println("InnerClass::func()");
            System.out.println(data1);
            //在非静态内部类当中，包含外部类的this
            System.out.println("======="+Outerclass2.this.data1);
            System.out.println(data2);
            System.out.println(data3);
            System.out.println(data4);
            System.out.println(data5);
            System.out.println(data6);

        }
    }
    public void test() {
        InnerClass2 innerClass2 = new InnerClass2();
        System.out.println(data1);
        System.out.println(data2);
        System.out.println(data3);
        System.out.println(innerClass2.data4);
        System.out.println(innerClass2.data5);
        System.out.println(innerClass2.data6);
    }
}
class Outerclass3 {

    public void func() {
        class InnerClass3 {//局部内部类
            public int a = 1;

            public void test() {
                System.out.println("HelloWorld");
            }
        }
        InnerClass3 innerClass3 = new InnerClass3();
        innerClass3.test();
    }
}
interface IA {
    void func();//不能实例化
}
class AA implements IA {//与匿名内部类等价
    @Override
    public void func() {
        System.out.println("HelloWorld");
    }
}

public class Test {
    public static void main(String[] args) {
        new IA(){//匿名内部类
            @Override
            public void func() {
                System.out.println("woqu");
            }
        }.func();
    }

    public static void main4(String[] args) {
        IA ia = new AA();
        ia.func();
    }

    public static void main3(String[] args) {
        Outerclass3 outerclass3 = new Outerclass3();
        outerclass3.func();
    }

    public static void main2(String[] args) {
        //实例内部类比较麻烦 需要外部类的对象
        Outerclass2 outerclass2 = new Outerclass2();
        Outerclass2.InnerClass2 innerClass2 = outerclass2.new InnerClass2();
        innerClass2.func();
    }

    public static void main1(String[] args) {
        //如何实例化静态内部类的对象
        Outerclass.InnerClass innerClass = new Outerclass.InnerClass();
        innerClass.func();
    }
}
